/*
	Author: 		夏の寒风
	Version:		1.0 (2013/09/27)
	QQ:				490720430
*/
(function($,window){
	$.fn.jsfoot_dialog = function(options) {
		var o = $.extend({},$.fn.jsfoot_dialog.options, options || {});
		var _x,_y,m;
		var box = 'stdmod-dialog', inside = 'stdmod-inside', header = 'stdmod-header', body = 'stdmod-body', background = 'stdmod-background', bg = 'stdmod-bg';
		var $background, $box, $header, $inside, $body;
		
		// 加载内容
		var loadContent = function() {
			var type = o.html ? 'html' : 'ajax';
			switch (type) {
				case 'html' :
					$body.html(o.html);
					break;
				case 'ajax' :
					$box.css({width: 200, height: 60});
					$body.html('<div class="loading">正在加载，请稍后…</div>');
					_url = o.ajax.url;
					_data = o.ajax.data;
					var ajaxLoader = $.ajax({
						cache: false,
						url	: _url,
						data : _data || {},
						error : function(XMLHttpRequest, textStatus, errorThrown) {
							if ( XMLHttpRequest.status > 0 ) {
								$body.html('<div class="loading-error">加载失败，请重试…</div>');
							}
						},
						success : function(data, textStatus, XMLHttpRequest) {
							var obj = typeof XMLHttpRequest == 'object' ? XMLHttpRequest : ajaxLoader;
							if (obj.status == 200) {
								$body.html(data);
								resizebox();
							}
						}
					});
				break;
			}
		};
		var resizebg = function() {
			if(o.bgvisibel) {
				var _width = $(document).width();
				var _height = $(document).height();
				$background.css({width: _width, height: _height});
			}
		};
		var resizebox = function() {
			$box.css({width: o.width, height: o.height});
			
			var marginLeft = parseInt(o.width/2);
			var marginTop = parseInt(o.height/2);
			var winWidth = parseInt($(window).width()/2);
			var winHeight = parseInt($(window).height()/2);
			var left = winWidth - marginLeft;
			var top = winHeight - marginTop;
			$box.css({left: left,top: top});
		}
		
		$(document).ready(function(e) {
			$('body').append('<div class="'+background+'"></div><div class="'+box+'"><div class="'+inside+'"><div class="'+header+'" onselectstart="return false;">'+o.title+'<a href="javascript:;"></a></div><div class="'+body+'"></div></div><div class="'+bg+'"></div></div>');
			$background = $('.' + background);
			$box = $('.' + box);
			$header = $box.find('.' + header);
			$inside = $box.find('.' + inside);
			$body = $box.find('.' + body);
			
			if(navigator.userAgent.indexOf('MSIE 7') > 0 || navigator.userAgent.indexOf('MSIE 8') > 0) {
				$box.css({'filter':'progid:DXImageTransform.Microsoft.gradient(startColorstr=#55000000,endColorstr=#55000000)'});
			}
			if(o.bgvisibel) {
				$background.fadeTo('fast',0.3);
				resizebg();
			}
			$box.show();
			resizebox();
			$header.find('a').click(function() {
				$background.remove();
				$box.remove();
			});
			$header.mousedown(function(e) {
				if(e.which) {
					m = true;
					_x = e.pageX - parseInt($box.css('left'));
					_y = e.pageY - parseInt($box.css('top'));
				}
			})
			loadContent();
		}).mousemove(function(e){
			if(m){
				var x = e.pageX - _x;
				var y = e.pageY - _y;
				$box.css({left:x, top:y});
			}
		}).mouseup(function(){
			m = false;
		});
		
		$(window).resize(function(){
			resizebg();
			resizebox();
		});
	};
	$.fn.jsfoot_dialog.options = {
		width: 400,
		height: 240,
		bgvisibel: true,
		title: 'Title',
		html: '',
		ajax: {
			url: '',
			data: {}
		}
	}
})(jQuery,window);